%macro get_f1040;

	/* Note: this assumes base_yr-1 = 2018. Otherwise, 
	needs new variable names for pension line */

	
	proc printto log = "&logdir/get_f1040.txt" new;
	run;
	
	proc sort data = rlib.filing_status(where=(prim_tin_x~=.)) out = unique nodupkey;
		by prim_tin_x;
	run;

	data _null_;
		set unique;
		file "&tmpdir/tinlist" dlm=',';
		put prim_tin_x;
	run;


	proc sql noprint;
		[connection details redacted]
			create table sample as
			select * from connection to iq 
		(
		select 
			a.prim_tin_x,
			b.[redacted] as adl_sd,
			b.[redacted] as cp,
			b.[redacted] as lag_eitc,
			b.[redacted] as lag_state_ref,
			b.[redacted] as lag_ui_inc,
			b.[redacted] as lag_all_ss,
			b.[redacted] as lag_gr_pens,
			b.[redacted] as lag_intincm,
			b.[redacted] as lag_divincm,
			b.[redacted] as lag_sch1_incm,
			b.[redacted] as lag_agi,
			case when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				when b.[redacted] > 0 then 1
				else 0 end as lag_inelig_ben
		from #tinlist as a 
			inner join [redacted] as b 
				on(a.prim_tin_x = b.[redacted])
		where b.[redacted] = &base_yr - 1
		order by a.prim_tin_x,
			b.[redacted]
	);
	disconnect from iq;
	quit;


	data rlib.f1040;
		set sample;
		by prim_tin_x;
		if last.prim_tin_x;
		drop cp;
	run;




	proc printto;
	run;

%mend;